using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using System.Windows.Forms;
using System.Threading;


namespace Mundo_3D
{

    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        Manejador_Modelos modelmanager;
        Manejador_Camaras cameramanager;
        Manejador_Mundo worldmanager;
        Form_3D form;
        float aspectRatio;
        Song sonido_helicoptero;
        Vector3 plataforma;
        string terreno;

        public Game1(Form_3D form)
        {
            this.form = form;
            Form xnaWindow = (Form)Control.FromHandle((this.Window.Handle));
            xnaWindow.GotFocus += new EventHandler(xnaWindow_GotFocus);
            form.Panel.Resize += new EventHandler(Panel_Resize);

            graphics = new GraphicsDeviceManager(this);
            graphics.PreparingDeviceSettings += new EventHandler<PreparingDeviceSettingsEventArgs>(graphics_PreparingDeviceSettings);
            aspectRatio = (float)form.Panel.Width / form.Panel.Height;
            Content.RootDirectory = "Content";
            terreno = "terreny";
            TargetElapsedTime = new TimeSpan(100000);  
        }

        public Game1(Form_3D form, Vector3 pla, string terr)
        {
            this.form = form;
            Form xnaWindow = (Form)Control.FromHandle((this.Window.Handle));
            xnaWindow.GotFocus += new EventHandler(xnaWindow_GotFocus);
            form.Panel.Resize += new EventHandler(Panel_Resize);

            graphics = new GraphicsDeviceManager(this);
            graphics.PreparingDeviceSettings += new EventHandler<PreparingDeviceSettingsEventArgs>(graphics_PreparingDeviceSettings);
            aspectRatio = (float)form.Panel.Width / form.Panel.Height;
            Content.RootDirectory = "Content";
            plataforma = new Vector3(pla.X,pla.Y,pla.Z);
            terreno = terr;
            TargetElapsedTime = new TimeSpan(100000);
        }


        void Panel_Resize(object sender, EventArgs e)
        {
            graphics.PreferredBackBufferWidth = form.Panel.Width;
            graphics.PreferredBackBufferHeight = form.Panel.Height;
            aspectRatio = (float)form.Panel.Width / form.Panel.Height;
            graphics.ApplyChanges();
        }

        void xnaWindow_GotFocus(object sender, EventArgs e)
        {
            ((Form)sender).Visible = false;
            form.TopMost = false;
        }

        void graphics_PreparingDeviceSettings(object sender, PreparingDeviceSettingsEventArgs e)
        {
            e.GraphicsDeviceInformation.PresentationParameters.DeviceWindowHandle = form.Panel.Handle;
        }

        protected override void Initialize()
        {
            cameramanager = new Manejador_Camaras(this);
            worldmanager = new Manejador_Mundo(this, cameramanager, terreno);
            modelmanager = new Manejador_Modelos(this, cameramanager);
            Components.Add(modelmanager);
            Components.Add(worldmanager);
            Components.Add(cameramanager);
            base.Initialize();
            modelmanager.AddModelos(new Nave(Content.Load<Model>(@"Models\quadmotor2"), Content.Load<Texture2D>(@"Textures/Quadrotor")));
            modelmanager.AddPlataforma(plataforma, Content.Load<Texture2D>(@"Textures/Plataforma"));
            modelmanager.AddCamaraSeguimiento(this);   
        }


        protected override void LoadContent()
        {   
            spriteBatch = new SpriteBatch(GraphicsDevice);
            sonido_helicoptero = Content.Load<Song>(@"Music/Helicoptero");
            MediaPlayer.IsRepeating = true;
            MediaPlayer.Play(sonido_helicoptero);
        }


        protected override void UnloadContent()
        {
            MediaPlayer.Stop();
        }


        protected override void Update(GameTime gameTime)
        {

            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == Microsoft.Xna.Framework.Input.ButtonState.Pressed)
                this.Exit();

            base.Update(gameTime);
        }


        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            base.Draw(gameTime);

        }
    }
}
